package com.socool.signup;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.android.FacebookError;
import com.facebook.model.GraphUser;
import com.socool.R;
import com.socool.R.anim;
import com.socool.R.id;
import com.socool.R.layout;
import com.socool.facebook.Act_Login_FB;

public class Act_SignUpSelect extends Activity {
	
	final static String LOG_TAG="test";
	
	private static final int FB_Login = 0; 
	
	ProgressDialog dialog;
	Bundle mBundle;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	  setContentView(R.layout.act_signup_select);
	  
	  Button BT_signup_facebook = (Button)findViewById(R.id.BT_signup_facebook);
	  Button BT_signup_normal = (Button)findViewById(R.id.BT_signup_normal);
	  
	  BT_signup_facebook.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {

			  Intent intent = new Intent(Act_SignUpSelect.this, Act_Login_FB.class);
			  startActivityForResult(intent, FB_Login);
			
				 
			
		}
	});
	  
	  BT_signup_normal.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent i = new Intent(Act_SignUpSelect.this, Act_Input_Number.class);
			startActivity(i);
			 overridePendingTransition(R.anim.fade,R.anim.hold);
		}
	});
	  
	  
	}
	
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent){
		Log.d(LOG_TAG, "["+getLocalClassName()+"]"+"onActivityResult");
		super.onActivityResult(requestCode, resultCode, intent);
		 
		switch(requestCode){
		case FB_Login: // requestCode가 FB_Login 케이스
		if(resultCode == RESULT_OK){ //B_ACTIVITY에서 넘겨진 resultCode가 OK일때만 실행
		
		
		 getInfo();
		
		
		}
		break;
		}
	}
	

	private void getInfo() {
		Log.d(LOG_TAG, "["+getLocalClassName()+"] getInfo()");
		Session session = Session.getActiveSession();
		if (session.isOpened()) {
			Log.d(LOG_TAG, "["+getLocalClassName()+"] getInfo() session is open");
			dialog = ProgressDialog.show(Act_SignUpSelect.this, "","Please wait..", true, true);
			Request.executeMeRequestAsync(session,
					new UserRequestListener());
		}else{
			Log.d(LOG_TAG, "["+getLocalClassName()+"] getInfo() session is not open");
		}
		
		
	}
	

	public class UserRequestListener implements Request.GraphUserCallback {

		@Override
		public void onCompleted(GraphUser user, Response response) {

			Log.d("test", "mJSONObject: " + user.getInnerJSONObject());

			JSONObject mJSONObject = user.getInnerJSONObject();

			String name_Id ="";
			String name_Email="";
			String name_Name = "";
			String name_Gender = "";
			String name_Work = "";
			String name_College = "";
			String name_Graduate_School = "";

			// Get Name
			name_Name = user.getName();

			// Get Gender
			try {
				name_Gender = mJSONObject.getString("gender");
				name_Email = mJSONObject.getString("email");
				name_Id = mJSONObject.getString("id");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("test", e.getMessage());
			}

			// Get Work
			try {
				JSONArray mJSONArray_Work = mJSONObject.getJSONArray("work");
				JSONObject JSONObject_Work = mJSONArray_Work.getJSONObject(0);
				name_Work = JSONObject_Work.getJSONObject("employer")
						.getString("name");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("test", e.getMessage());
			}

			// Get Education
			boolean ok_college = false;
			boolean ok_graduate = false;
			try {
				JSONArray mJSONArray_Education = mJSONObject
						.getJSONArray("education");
				for (int i = 0; i < mJSONArray_Education.length(); i++) {
					JSONObject JSONObject_Education = mJSONArray_Education
							.getJSONObject(i);
					String type = JSONObject_Education.getString("type");

					if (type.equals("College") && !ok_college) {
						name_College = JSONObject_Education.getJSONObject(
								"school").getString("name");
						ok_college = true;

					} else if (type.endsWith("Graduate School") && !ok_graduate) {
						name_Graduate_School = JSONObject_Education
								.getJSONObject("school").getString("name");
						ok_graduate = true;
					}

				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("test", e.getMessage());
			}

			String str = "Name: " + name_Name;
			str += "\nId: " + name_Id;
			str += "\nEmail: " + name_Email;
			str += "\nGender: " + name_Gender;
			str += "\nWork: " + name_Work;
			str += "\nCollege: " + name_College;
			str += "\nGraduate School: " + name_Graduate_School;
			
			Log.d(LOG_TAG, str);
			
		
			
			
			dialog.dismiss();
			//Intent i = new Intent(Act_SignUpSelect.this, Act_SignUpForm.class);
			
			
			/*
			i.putExtra("Id", name_Id);
			i.putExtra("Email", name_Email);
			i.putExtra("Name", name_Name);
			i.putExtra("Gender", name_Gender);
			i.putExtra("Work", name_Work);
			i.putExtra("College", name_College);
			i.putExtra("Graduate", name_Graduate_School);
			*/
			
			Intent i = new Intent(Act_SignUpSelect.this, Act_Input_Number.class);
			Bundle b = new Bundle();
			
			b.putString("Id", name_Id);
			b.putString("Email", name_Email);
			b.putString("Name", name_Name);
			b.putString("Gender", name_Gender);
			b.putString("Work", name_Work);
			b.putString("College", name_College);
			b.putString("Graduate", name_Graduate_School);
			
			i.putExtras(b);
			startActivity(i);
			 overridePendingTransition(R.anim.fade,R.anim.hold);
			

		}

		public void onFacebookError(FacebookError error) {
			dialog.dismiss();
			Toast.makeText(getApplicationContext(),
					"Facebook Error: " + error.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}

	}
	

}
